﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using System;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;

namespace Minigolf.Gamestates
{
    public class GolfBall
    {
        public Texture2D Texture;
        private Vector2 Position;
        private float Radius;
        private Vector2 Velocity;
        private float Friction = 0.98f;

        public GolfBall()
        {
        }

        public void SetRadius(float Rad)
        {
            Radius = Rad;
        }

        public float GetRadius()
        {
            return Radius;
        }

        public void SetPos(Vector2 Pos)
        {
            Position = Pos;
        }

        public Vector2 GetPos()
        {
            return Position;
        }

        public void SetVelocity(Vector2 Vel)
        {
            Velocity = Vel;
        }

        public void Update(GameTime gameTime)
        {
            Velocity *= Friction;
            Position += Velocity;
        }
    }

    public class SinglePlayer : BaseState
    {
        public GolfBall Player1;
        private bool Pressed;
        private bool BallTouched;

        public SinglePlayer()
        {
            Player1 = new GolfBall();
            Player1.Texture = Game1.content.Load<Texture2D>("golfball");
            Player1.SetRadius(1);
            Player1.SetPos(new Vector2(5, 5));
        }

        public override void Init(Game source)
        {

        }

        public override void Update(GameTime gameTime)
        {
            Camera.Update(gameTime);

            Player1.Update(gameTime);

            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                if (!Pressed)
                {
                    Pressed = true;

                    Vector2 DrawPos = Camera.DrawPos(Player1.GetPos());
                    Vector2 Size = new Vector2(Camera.CakeToPixel(Player1.GetRadius() / 2), Camera.CakeToPixel(Player1.GetRadius() / 2));
                    Rectangle Rect = new Rectangle((int)DrawPos.X, (int)DrawPos.Y, (int)Size.X, (int)Size.Y);

                    if (Rect.IntersectsPoint(new Vector2(Mouse.GetState().X, Mouse.GetState().Y)))
                    {
                        BallTouched = true;
                    }
                }
            }

            if (Mouse.GetState().LeftButton == ButtonState.Released)
                if (Pressed)
                {
                    Pressed = false;

                    if (BallTouched)
                    {
                        BallTouched = false;

                        Vector2 MousePos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
                        Vector2 DrawPos = Camera.DrawPos(Player1.GetPos() + new Vector2(Player1.GetRadius() / 4, Player1.GetRadius() / 4));
                        float Size = Vector2.Distance(MousePos, DrawPos) / 500;

                        Player1.SetVelocity(-(Vector2.Normalize(MousePos - DrawPos) * Size));
                    }
                }
        }

        public override void Render(RenderStage render, SpriteBatch batch)
        {
            {
                Vector2 DrawPos = Camera.DrawPos(Player1.GetPos());
                Vector2 Size = new Vector2(Camera.CakeToPixel(Player1.GetRadius() / 2), Camera.CakeToPixel(Player1.GetRadius() / 2));

                batch.Begin();
                batch.Draw(Player1.Texture, new Rectangle((int)DrawPos.X, (int)DrawPos.Y, (int)Size.X, (int)Size.Y), Color.White);
                batch.End();
            }

            if (BallTouched)
            {
                Vector2 MousePos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
                Vector2 DrawPos = Camera.DrawPos(Player1.GetPos() + new Vector2(Player1.GetRadius() / 4, Player1.GetRadius() / 4));
                float Rot = (float)Math.Atan2(MousePos.Y - DrawPos.Y, MousePos.X - DrawPos.X);
                float Size = Vector2.Distance(MousePos, DrawPos);

                batch.Begin();
                batch.Draw(Game1.ShapeRectangle, new Rectangle((int)DrawPos.X, (int)DrawPos.Y, (int)Size, 2), new Rectangle(0, 0, 32, 7), Color.Orange, Rot, new Vector2(0, 3), SpriteEffects.None, 0f);
                batch.End();
            }
        }

        public override void Shutdown()
        {
   
        }
    }

}
